/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.control;

import java.util.List;

import org.mags.common.EngineContext;
import org.mags.remas.control.commons.ControlUtils;
import org.mags.remas.logic.EntityLogic;
import org.mags.remas.model.entity.EntityCategory;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.view.widgets.TreeActionTransmitter;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class FindEntityViewController extends EntityRelationshipController {	
	private EntityRelationship filter;
	private int requiredPage=0;
	private int pageSize=100;
	private long totalSize = 0;
	
	private int getLastPage() {
		int lastPage = (int)(totalSize/pageSize)-1;
		if((totalSize%pageSize)>0)
			lastPage = lastPage+1;
		return lastPage;
	}
	public void moveNextPage() {
		if(requiredPage+1<=getLastPage())
			requiredPage++;
	}
	public void movePrevPage() {
		if(requiredPage-1>=0)
			requiredPage--;
	}
	public void moveLastPage() {
		requiredPage = getLastPage();
	}
	public void moveFirstPage() {
		requiredPage = 0;
	}
	public long getTotalSize() {
		return totalSize;
	}
	public int getRequiredPage() {
		return requiredPage;
	}
	public void setRequiredPage(int requiredPage) {
		this.requiredPage = requiredPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public EntityRelationship getFilter() {
		return filter;
	}
	public void setFilter(EntityRelationship filter) {
		this.filter = filter;
		requiredPage = 0;
	}
	public FindEntityViewController() {
	}
	public boolean hasChildren(Object object) {
		return false;
	}
	public Object[] getChildren(Object object) {
		return null;
	}
	public Object[] getElements(Object object) {
		totalSize = EngineContext.getDataContext().getEntityProviderFactory().getEntityRelationshipProvider().countBySample(filter);
		return EngineContext.getDataContext().getEntityProviderFactory().getEntityRelationshipProvider().findBySample(filter, requiredPage*pageSize, pageSize).toArray();
	}
	public EntityRelationship refresh(EntityRelationship entityRelationship) {
		EntityRelationship er = EngineContext.getDataContext().getEntityProviderFactory().getEntityRelationshipProvider().getById(entityRelationship.getEntityRelationshipId());
		TreeActionTransmitter.getInstance().updateData(er);
		return er;
	}
	public void delete(List<EntityRelationship> relationshipsToDelete) {
		for(EntityRelationship entityRelationshipToDelete: relationshipsToDelete) {
			EntityRelationship parentEntityRelationship = entityRelationshipToDelete.getParent();
			EntityCategory entityCategory = entityRelationshipToDelete.getEntity().getEntityType().getEntityCategory();
			if(parentEntityRelationship==null) {
				parentEntityRelationship = new EntityRelationship();
				parentEntityRelationship.setEntity(entityRelationshipToDelete.getEntityParent());
				String hierarchicalPath = ControlUtils.getParentPath(entityRelationshipToDelete.getHierarchicalPath(), entityRelationshipToDelete.getEntity().getEntityId());
				parentEntityRelationship.setHierarchicalPath(hierarchicalPath);
			}
			EntityLogic.getInstance().delete(entityRelationshipToDelete, parentEntityRelationship, entityCategory);
		}		
	}	
}
